//
//  ViewController.m
//  AppleScript
//
//  Created by My mac on 2019/4/4.
//  Copyright © 2019年 VIPCODE. All rights reserved.
//

#import "ViewController.h"
#import <Carbon/Carbon.h> // for AppleScript definitions

#define kASAppleScriptSuite 'ascr'
#define kASSubroutineEvent  'psbr'
#define keyASSubroutineName 'snam'

@interface ViewController ()

@property (assign, nonatomic) CGRect windowFrame;

@end

#define VIPScreenVisibleWidth           [NSScreen mainScreen].visibleFrame.size.width
#define VIPScreenVisibleHeight          [NSScreen mainScreen].visibleFrame.size.height
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


- (IBAction)didTapOpen:(id)sender {
    // run the script from the application's bundle
    
    NSURL *URL = [[NSBundle mainBundle] URLForResource:@"CallBrowser" withExtension:@"scpt"];
    if (URL) {
        NSAppleScript *appleScript = [[NSAppleScript alloc] initWithContentsOfURL:URL error:NULL];
        
        NSString *inputString = @"https://test-robot.vipcode.com/live/?clientFlag=1&pkSyllabus=ECB4BFB988C3D5DF92EC337445A9C9DD&pkUser=6C17965883CB1FF1A1DA2F7C219DC838";
        
        NSAppleEventDescriptor *parameter = [NSAppleEventDescriptor descriptorWithString:inputString];
        NSAppleEventDescriptor *parameters = [NSAppleEventDescriptor listDescriptor];
        [parameters insertDescriptor:parameter atIndex:1];
        
        ProcessSerialNumber psn = {0, kCurrentProcess};
        NSAppleEventDescriptor *target = [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber bytes:&psn length:sizeof(ProcessSerialNumber)];
        
        NSAppleEventDescriptor *function = [NSAppleEventDescriptor descriptorWithString:@"openURL"];
        
        NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite eventID:kASSubroutineEvent targetDescriptor:target returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
        [event setParamDescriptor:function forKeyword:keyASSubroutineName];
        [event setParamDescriptor:parameters forKeyword:keyDirectObject];
        
        NSDictionary *error = nil;
        [appleScript executeAppleEvent:event error:&error];
        if(error) {
            NSLog(@"%@", error);
        }
    }
}


@end
